<template>
  <div>
    <el-collapse v-model="activeNames">
      <el-form
        ref="form"
        :model="form"
        :label-width="labelWidth"
        :rules="rules"
      >
        <el-collapse-item
          v-for="items in collapseItem"
          :key="items.name"
          :name="items.name"
        >
          <template slot="title">
            <i :class="items.icon"></i>
            <h3>{{ items.title }}</h3>
          </template>
          <el-row>
            <el-col
              v-for="(item, index) in items.formList"
              :key="index"
              :span="item.span"
            >
              <el-form-item :label="item.label + '：'">
                <el-input
                  v-model="form[item.prop]"
                  v-if="item.type === 'Input'"
                  :placeholder="item.placeholder"
                ></el-input>
                <el-select
                  :multiple="item.isShow"
                  :filterable="item.isShow"
                  :allow-create="item.isShow"
                  :default-first-option="item.isShow"
                  :placeholder="item.placeholder"
                  v-if="item.type === 'Select'"
                  v-model="form[item.prop]"
                >
                  <el-option
                    v-for="(item, index) in item.options"
                    :key="index"
                    :value="item.value"
                    :label="item.label"
                  >
                  </el-option>
                </el-select>
                <slot
                  v-if="item.type === 'transfer'"
                  :name="item.slotName"
                ></slot>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-form>
    </el-collapse>
  </div>
</template>

<script>
export default {
  props: {
    collapseItem: {
      type: Array,
      default: () => [],
    },
    icon: {
      type: String,
    },
    title: {
      type: String,
    },
    value: {
      type: Array,
      default: () => [],
    },
    name: {
      type: String,
    },
    rules: {
      type: Array,
      default: () => [],
    },
    labelWidth: {
      type: String,
    },
  },
  data() {
    return {
      activeNames: [],
      form: {},
    };
  },
  mounted() {
    this.getName();
  },
  methods: {
    getName() {
      this.collapseItem.forEach((i) => {
        this.activeNames.push(i.name);
      });
    },
    initFormData() {
      const formData = {};
      this.collapseItem.forEach((item) => {
        item.formList.filter((items) => {
          if (items.prop) {
            formData[items.prop] = items.value || "";
          }
        });
      });
      this.form = formData;
      console.log(this.form);
    },
  },
  watch: {
    collapseItem: {
      handler() {
        this.initFormData();
      },
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped>
.dialog1 {
  font-size: 20px;
  margin-right: 10px;
}
.el-select {
  width: 100%;
}
</style>
